/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TSupplier;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TSupplierDao;
import com.ruipengkj.service.TSupplierService;

/**
 * 说明： 供货商单Service层接口
 * 
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TSupplierServiceImpl implements TSupplierService {
	@Resource
	private TSupplierDao TSupplierDao;// DAO层

	// 查询所有
	@Override
	public List<TSupplier> findService(TSupplier entity) throws Exception {
		String hql = "select o from TSupplier o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getSupplierID() != null
					&& !entity.getSupplierID().equals("")) {
				whereHQL.append("supplierID").append(BaseQuery.LIKE)
						.append("?").append(BaseQuery.AND);
				param.add("%" + entity.getSupplierID() + "%");
			
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TSupplierDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
	
	
	/** 查出所有有效的共华商 **/
	@Override
	public List<TSupplier> findValidService(TSupplier entity) throws Exception {
		String hql = "select o from TSupplier o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		
		whereHQL.append("o.isValid=?");
		param.add(true);
		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TSupplierDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	// 查询所有
	@Override
	public List<TSupplier> findByNameService(String name) throws Exception {
		String hql = "select new TSupplier(o.supplierID) from TSupplier o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (name != null && !name.equals("")) {
			whereHQL.append("name=?");
			param.add(name);
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TSupplierDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	// 根据ID翻页查询
	@Override
	public PageModel<TSupplier> getScrollService(int firstResult,
			int maxResult, TSupplier entity) throws Exception {
		String hql = "select o from TSupplier o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getSupplierID() != null
					&& !entity.getSupplierID().equals("")) {
				whereHQL.append("supplierID").append(BaseQuery.LIKE)
						.append("?").append(BaseQuery.AND);
				param.add("%" + entity.getSupplierID() + "%");
			}
			if (entity.getName() != null && !entity.getName().equals("")) {
				whereHQL.append("name").append(BaseQuery.LIKE).append("?")
						.append(BaseQuery.AND);
				param.add("%" + entity.getName() + "%");
			}
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TSupplierDao.getScrollDao(hql, whereHQL.toString(), param,
				firstResult, maxResult, orderby);
	}

	// 修改
	@Override
	public void modifyService(TSupplier entity) throws Exception {
		TSupplierDao.modifyDao(entity);

	}

	// 添加
	@Override
	public void saveService(TSupplier entity) throws Exception {
		TSupplierDao.saveDao(entity);
	}

	// 根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TSupplierDao.deleteDao(id);

	}

	// 根据ID查询
	@Override
	public TSupplier getByIdService(String id) throws Exception {
		return TSupplierDao.getByIdDao(id);
	}

	// 添加或修改
	@Override
	public void saveOrUpdateService(TSupplier entity) throws Exception {
		TSupplierDao.saveOrUpdateDao(entity);
	}

	// 添加返回ID
	@Override
	public Serializable saveRetService(TSupplier entity) throws Exception {
		return TSupplierDao.saveRetDao(entity);
	}

	public TSupplierDao getTSupplierDao() {
		return TSupplierDao;
	}

	public void setTSupplierDao(TSupplierDao TSupplierDao) {
		this.TSupplierDao = TSupplierDao;
	}

	@Override
	public List<TSupplier> findExportService(TSupplier entity) throws Exception {
		String hql = "select o from TSupplier o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getSupplierID() != null
					&& !entity.getSupplierID().equals("")) {
				whereHQL.append("supplierID").append(BaseQuery.LIKE)
						.append("?").append(BaseQuery.AND);
				param.add("%" + entity.getSupplierID() + "%");
			}
			if (entity.getName() != null && !entity.getName().equals("")) {
				whereHQL.append("name").append(BaseQuery.LIKE).append("?")
						.append(BaseQuery.AND);
				param.add("%" + entity.getName() + "%");
			}
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TSupplierDao.findDao(hql, whereHQL.toString(), param, orderby);
	}
}